<template>
  <!-- 导入基本数据 -->
  <a-modal
    :visible="importExeclVisible"
    title="家庭成员数据待确认表"
    centered
    width="80%"
    dialogClass='member-execl-dialog'
    @cancel="importExeclVisible = false"
  >
    <ImportExeclPreview :execlData="execlData" ref="ImportExeclPreview" />
    <section slot="footer">
      <a-button
        @click="importExeclVisible = false"
      >取消</a-button>
      <a-button
        type="primary"
        :loading="importExeclConfirmLoading"
        @click="handlerConfirmImport"
      >{{ isPreview ? '待上传文件内容已确认' : '确认导入' }}</a-button>
    </section>
  </a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import { ImportExecl } from '@/utils/execl'
import { famerServices, IMember } from '@/common/services/famer'
import ImportExeclPreview from './FamilyImportExeclPreview.vue'
import { message, Modal } from 'ant-design-vue'
import { isIDCard } from '@/utils/tools'

@Component({
  components: {
    ImportExeclPreview
  }
})
export default class MemberImportMixin extends Vue {
  @Prop({
    type: Boolean
  })
  public isPreview!: boolean
  public file!: File
  // 上传execl
  public importExeclVisible = false
  public importExeclConfirmLoading = false
  public execlData: any[] = []
  public handlerUploadExecl() {
    const _self = this
    ImportExecl({
      member_id: '编号',
      member_nam: '姓名',
      member_sfz: '身份证号',
      member_sex: '性别',
      // member_sex: '出生日期',
      role: '与户主关系',
      member_bank_num: '银行账号',
      member_bank_code: '开户银行',
      readme: '备注'
    }).then(_ => {
      this.importExeclVisible = true
      // @ts-ignore
      this.execlData = this.formatIDCardError(_.formatedData) || [];
      // @ts-ignore
      this.file = _.file
    })
  }

  public formatIDCardError(data: any[]) {
    return data.map(item => {
      if (!isIDCard(item.member_sfz)) {
        return {
          ...item,
          error: true
        }
      }
      return item
    })
  }

  public async handlerConfirmImport() {
    // @ts-ignore
    const data = this.$refs.ImportExeclPreview.execlData
    if (!data.length) {
      message.error("请导入数据")
      return false
    }

    // 预览模式下直接返回数据 + 关闭
    if (this.isPreview) {
      this.$emit('previewSuccess', {
        data,
        file: this.file
      })
      this.importExeclVisible = false
      return
    }

    this.importExeclConfirmLoading = true

    try {
      const result = await famerServices.importFamilyExecl({
        data,
        file_nam: this.file.name
      })
      Modal.success({
        content: result.data || '数据导入成功',
        centered: true
      })
    } catch(e) {
      // Modal.info({
      //   content: e.data || '数据导入成功',
      //   centered: true
      // })
      throw e;
    } finally {
      this.importExeclVisible = false
      this.importExeclConfirmLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
.member-container {
  display: flex;
  .area-tree {
    width: 300px;
    flex: none;
    background-color: #fff;
    border-right: 1px solid #eee;
    margin-right: 10px;
  }
}
.house-holder /deep/ .ant-card-body {
  max-height: 600px;
  height: auto;
}
.member-list {
  display: flex;
  flex-direction: column;
}
</style>
